//
//  UIImage+Watermark.m
//  huimei
//
//  Created by fengyn on 16/1/28.
//  Copyright © 2016年 lexingsoft. All rights reserved.
//

#import "UIImage+Watermark.h"

@implementation UIImage(Watermark)

///修改图片大小
- (UIImage *)resetImageSize
{
#define MaxWidth 1000
    CGSize size = self.size;
    CGSize contextSize=CGSizeMake(MaxWidth, size.height*(MaxWidth/size.width));
    UIGraphicsBeginImageContext(contextSize);
    [self drawInRect:CGRectMake(0, 0, contextSize.width, contextSize.height)];
    UIImage *aimg = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return aimg;
}

///根据给定文字增加九宫格水印
- (UIImage *)addWatermarkString:(NSString *)watermarkString{
    UIFont *theFont=[UIFont systemFontOfSize:27];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init] ;
    [paragraphStyle setAlignment:NSTextAlignmentLeft];
    NSShadow *shadow=[[NSShadow alloc] init];
    shadow.shadowBlurRadius=4;
    shadow.shadowColor=[UIColor redColor];
    NSDictionary *attributes =@{NSFontAttributeName:theFont,
                                NSForegroundColorAttributeName:[UIColor colorWithWhite:1 alpha:0.4],
                                NSShadowAttributeName:shadow,
                                NSParagraphStyleAttributeName:paragraphStyle};
    UIImage *textImage=[self imageWithText:watermarkString attributes:attributes];
    textImage=[self image:textImage rotatedByDegrees:35];
    NSDictionary *attributes1 =@{NSFontAttributeName:theFont,
                                NSForegroundColorAttributeName:[UIColor colorWithWhite:1 alpha:0.9],
                                NSShadowAttributeName:shadow,
                                NSParagraphStyleAttributeName:paragraphStyle};
    UIImage *textImage1=[self imageWithText:watermarkString attributes:attributes1];
    textImage1=[self image:textImage1 rotatedByDegrees:35];
    UIImage *scalImg=[self resetImageSize];
    return [self img:scalImg addImg:textImage addImg:textImage1];
}

///根据给定文字增加右下角水印
- (UIImage *)addOneWatermarkString:(NSString *)watermarkString{
    UIFont *theFont=[UIFont systemFontOfSize:30];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init] ;
    [paragraphStyle setAlignment:NSTextAlignmentRight];
    NSShadow *shadow=[[NSShadow alloc] init];
    shadow.shadowBlurRadius=4;
    shadow.shadowColor=[UIColor redColor];
    NSDictionary *attributes =@{NSFontAttributeName:theFont,
                                NSForegroundColorAttributeName:[UIColor colorWithWhite:1 alpha:0.6],
                                NSShadowAttributeName:shadow,
                                NSParagraphStyleAttributeName:paragraphStyle};
    UIImage *textImage=[self imageWithText:watermarkString attributes:attributes];
    UIImage *scalImg=[self resetImageSize];
    return [self img:scalImg addOneImg:textImage];
}

///旧生成水印图片
- (UIImage *)imageWithText:(NSString *)mark attributes:(NSDictionary *)attributes{
    CGSize textSize=[mark sizeWithAttributes:attributes];
    
    UIGraphicsBeginImageContext(textSize);
    //    [mark drawInRect:CGRectMake(0,0,textSize.width,textSize.height) withFont:theFont];
    [mark drawInRect:CGRectMake(0,0,textSize.width,textSize.height) withAttributes:attributes];
    UIImage *aimg = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return aimg;
}

///计算文本Size
- (CGSize)calcSizeText:(NSString *)text font:(UIFont *)font
{
    NSDictionary *attributes = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
    CGSize textSize = [text sizeWithAttributes:attributes];
    return textSize;
}

///增加单张图片水印
- (UIImage *)img:(UIImage *)img addOneImg:(UIImage *)theImg{
    CGFloat w = img.size.width;
    CGFloat h = img.size.height;
    UIGraphicsBeginImageContext(img.size);
    [img drawInRect:CGRectMake(0, 0, w, h)];
    CGSize textSize=theImg.size;
    CGRect r=CGRectMake((w-textSize.width-10),(h-textSize.height-10), textSize.width, textSize.height);
    [theImg drawInRect:r];
    UIImage *aimg = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return aimg;
}

///九宫格增加图片水印
- (UIImage *) img:(UIImage *)img addImg:(UIImage *)theImg addImg:(UIImage *)theImg2{
    CGFloat w = img.size.width;
    CGFloat h = img.size.height;
    CGFloat perW=w/3;
    CGFloat perH=h/3;
    UIGraphicsBeginImageContext(img.size);
    [img drawInRect:CGRectMake(0, 0, w, h)];
    CGSize textSize=theImg.size;
    for (int i = 0; i<3; i++) {
        for (int j = 0; j<3; j++) {
            CGRect r=CGRectMake((perW * j) + ((perW - textSize.width)/2),(perH * i) + ((perH - textSize.height)/2), textSize.width, textSize.height);
            if ((i==0&&j==0)||(i==2&&j==2)||(i==0&&j==2)||(i==2&&j==0)) {
                [theImg2 drawInRect:r];
            }else{
                [theImg drawInRect:r];
            }
        }
    }
    UIImage *aimg = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return aimg;
}

///将image转90,180,270度的方法
- (UIImage *)image:(UIImage *)image rotation:(UIImageOrientation)orientation
{
    long double rotate = 0.0;
    CGRect rect;
    float translateX = 0;
    float translateY = 0;
    float scaleX = 1.0;
    float scaleY = 1.0;
    
    switch (orientation) {
        case UIImageOrientationLeft:
            rotate = 1 * M_PI_2;
            rect = CGRectMake(0, 0, image.size.height, image.size.width);
            translateX = 0;
            translateY = -rect.size.width;
            scaleY = rect.size.width/rect.size.height;
            scaleX = rect.size.height/rect.size.width;
            break;
        case UIImageOrientationRight:
            rotate = 3 * M_PI_2;
            rect = CGRectMake(0, 0, image.size.height, image.size.width);
            translateX = -rect.size.height;
            translateY = 0;
            scaleY = rect.size.width/rect.size.height;
            scaleX = rect.size.height/rect.size.width;
            break;
        case UIImageOrientationDown:
            rotate = M_PI;
            rect = CGRectMake(0, 0, image.size.width, image.size.height);
            translateX = -rect.size.width;
            translateY = -rect.size.height;
            break;
        default:
            rotate = 0.0;
            rect = CGRectMake(0, 0, image.size.width, image.size.height);
            translateX = 0;
            translateY = 0;
            break;
    }
    
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    //做CTM变换
    CGContextTranslateCTM(context, 0.0, rect.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);
    CGContextRotateCTM(context, rotate);
    CGContextTranslateCTM(context, translateX, translateY);
    
    CGContextScaleCTM(context, scaleX, scaleY);
    //绘制图片
    CGContextDrawImage(context, CGRectMake(0, 0, rect.size.width, rect.size.height), image.CGImage);
    
    UIImage *newPic = UIGraphicsGetImageFromCurrentImageContext();
    
    return newPic;
}

///角度转弧度
-(CGFloat)degreesToRadians:(CGFloat)degress
{
    return degress*M_PI/180.0;
}

///生成旋转给定角度之后的图片
- (UIImage *)image:(UIImage *)image rotatedByDegrees:(CGFloat)degrees
{
    CGSize size = image.size;
    CGSize contextSize=CGSizeMake(size.width+size.height, size.width+size.height);
    UIGraphicsBeginImageContext(contextSize);
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGAffineTransform transform=CGContextGetCTM(ctx);
    transform = CGAffineTransformRotate(transform, [self degreesToRadians:degrees]);
    transform = CGAffineTransformTranslate(transform,contextSize.width/4,-contextSize.height/3);
    CGContextConcatCTM(ctx, transform);
    //    CGContextRotateCTM(ctx, -[self degreesToRadians:45.]);
    //    CGContextScaleCTM(ctx, -1, 1);
    //    CGContextTranslateCTM(ctx,-contextSize.width*4, -contextSize.width*4);
    CGContextDrawImage(ctx, CGRectMake((contextSize.width-size.width)/2,(contextSize.height-size.height)/2,size.width, size.height), image.CGImage);
    UIImage *newPic = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newPic;
}

///根据颜色和大小生成图片
+ (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size
{
    @autoreleasepool {
        
        CGRect rect = CGRectMake(0, 0, size.width, size.height);
        
        UIGraphicsBeginImageContext(rect.size);
        
        CGContextRef context = UIGraphicsGetCurrentContext();
        
        CGContextSetFillColorWithColor(context,
                                       
                                       color.CGColor);
        
        CGContextFillRect(context, rect);
        
        UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
        
        UIGraphicsEndImageContext();
        
        
        
        return img;
        
    }
}

@end
